import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;

import java.io.IOException;

//删除指定列
public class DeleteDemo {
    static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://192.168.126.139:9000/hbase");
        conf.set("hbase.master", "hdfs://192.168.126.139:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "192.168.126.139");
    }

    public static boolean deleteQualifier(String tableName, String rowName, String columnFamilyName, String qualifierName) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        HTable table = new HTable(conf, tableName);
        if (admin.tableExists(tableName)) {
            try {
                Delete delete = new Delete(rowName.getBytes());
                delete.deleteColumn(columnFamilyName.getBytes(), qualifierName.getBytes());
                table.delete(delete);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("删除失败");
                return false;
            }
        }
        System.out.println("删除成功");
        return true;
    }

    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        deleteQualifier("student", "rowkey3", "person", "sex");
    }
}
